package com.qiaoyanqing.textdemoqyq7.Activities;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;


import com.qiaoyanqing.textdemoqyq7.AsyncTasks.Web2AsyncTask;

import com.qiaoyanqing.textdemoqyq7.Beans.WebBeans;
import com.qiaoyanqing.textdemoqyq7.CallBacks.Web2CB;

import com.qiaoyanqing.textdemoqyq7.R;
import com.qiaoyanqing.textdemoqyq7.SQL.MyCollegeHelper;

import java.util.ArrayList;
import java.util.List;

public class WebViewActivity extends AppCompatActivity {
    private WebView mWebView;

    private List<WebBeans>mData=new ArrayList<>();

    private static final String BASE_URL = "http://sns.maimaicha.com/api?apikey=b4f4ee31a8b9acc866ef2afb754c33e6&format=json&method=";
    //内容详情页，id为对应文章id，主界面Json数据中获取
    public static final String DETAIL_URL = BASE_URL + "news.getNewsContent&id=";

    private TextView mtitle,msource,mcreatetime;



    private MyCollegeHelper mHelper;


//TODO
    //private int num;
    private String mPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        mWebView= (WebView) findViewById(R.id.webview);
        mHelper=new MyCollegeHelper(this);
        mtitle= (TextView) findViewById(R.id.web_title);
        msource= (TextView) findViewById(R.id.web_source);
        mcreatetime= (TextView) findViewById(R.id.web_create_time);


        //接收数据
//        Bundle bundle=this.getIntent().getExtras();
//        num=bundle.getInt("page");
        Intent intent=getIntent();
        mPage = intent.getStringExtra("page");


        initWebview();
        //加载数据
        initData();

    }

    private void initWebview() {
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setInitialScale(90);
     //   mWebView.setTextAlignment(100);
//        mWebView.setLayoutParams();
//       mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
//       mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setTextSize(WebSettings.TextSize.LARGEST);
    }
//    public static final WebSettings.TextSize[]FONT_SIZE=new WebSettings.TextSize[]{
//            WebSettings.TextSize.NORMAL,
//            WebSettings.TextSize.LARGEST,
//            WebSettings.TextSize.LARGER
//    };





    private void initData() {
        new Web2AsyncTask(new Web2CB() {
            @Override
            public void senddata(List<WebBeans> data) {
                mData.addAll(data);
               // String url= mData.get(0).getWeiboUrl();
              //  Log.d("senddata", "senddata: "+url);
                mtitle.setText(mData.get(0).getTitle());
                msource.setText(mData.get(0).getSource());
                mcreatetime.setText(mData.get(0).getCreate_time());

                mWebView.loadDataWithBaseURL(null,mData.get(0).getWap_content(),"text/html","utf-8",null);

            }
        }).execute(DETAIL_URL+mPage);

    }


    //返回
    public void back(View view) {
//        Intent intent=new Intent(WebViewActivity.this,MainActivity.class);
//        startActivity(intent);

          finish();

    }

    //收藏到数据库
    public void college(View view) {
        SQLiteDatabase db=mHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("id",mData.get(0).getId());
        values.put("title",mData.get(0).getTitle());
        values.put("source",mData.get(0).getSource());
        values.put("create_time",mData.get(0).getCreate_time());
        db.insert("shoucang",null,values);
        Toast.makeText(this, "收藏成功", Toast.LENGTH_SHORT).show();

    }
    //分享一下
    public void share(View view) {
        Toast.makeText(this, "分享成功", Toast.LENGTH_SHORT).show();
    }



}
